<template>
	<view class="goods-details">
	<u-sticky offset-top="0" h5-nav-height="0" bg-color="transparent" :enable="enable">
	    <u-navbar
			title="详情"
			titleColor="#FFF"
	        :border-bottom="false"
	        :is-fixed="false"
	        custom-class="nav-bg"
			:background="{background: '#116568'}"
			back-icon-color="#FFF"
	    > 
	    </u-navbar>
	</u-sticky>
		<!-- #ifdef H5 -->
		<download-nav v-if="showDownload" :top="44"></download-nav>
		<!-- #endif -->
		<loading-view v-if="isFirstLoading"></loading-view>
		<view class="contain" v-if="!isNull">
			<bubble-tips top="180rpx"></bubble-tips>
			<product-swiper :imgUrls="swiperList" :video="goodsDetail.video"></product-swiper>
			<view class="product-box">
				<view class="title">{{goodsDetail.title}}</view>
				<view class="content">
					<u-parse :html="goodsDetail.content" :lazy-load="true" :show-with-animation="true"></u-parse>
				</view>
				<!-- <view class="desc">{{goodsDetail.remark}}</view> -->
				<!-- 适用门店 -->
				<view class="store-box">
					<view class="store-item row-between">
						<view class="left row">
							<image :src="goodsDetail.shop_image" />
							<view class="info">
								<view class="info-tit">{{goodsDetail.shop_name}}</view>
								<view class="address">{{goodsDetail.shop_address}}</view>
							</view>
						</view>
						<view class="right row">
								<view class="right-item column" @tap="handleNavigation(goodsDetail.shop_latitude,goodsDetail.shop_longitude)">
									<image :src="`${imgPrefix}/images/xcxweb/details/navigation.png`" />
									导航
								</view>
								<view class="right-item column" @tap="handleCall(goodsDetail.shop_mobile)">
									<image :src="`${imgPrefix}/images/xcxweb/details/phone.png`" />
									电话
								</view>
						</view>
					</view>
				</view>
			</view>
			
			
		<!-- 	<view class="details mt20 bg-white">
				<view class="title lg">商品详情</view>
				<view class="content">
					<u-parse :html="goodsDetail.content" :lazy-load="true" :show-with-animation="true"></u-parse>
				</view>
			</view> -->
			
			<!-- 暂时隐藏 -->
			<!-- <view class="footer row-between bg-white fixed">
				<view class="input-box row">
					<image  src="../../static/images/edit.png" class="edit-img"></image>
					<input placeholder="发表评论" :placeholder-style="{color: '#999'}"	/>
				</view>
				<view class="num-box row">
					<view class="collect-box row">
						<image  src="../../static/images/collect.png" class="collect-img"></image>
						<text>74123</text>
					</view>
					<view class="comment-box row">
						<image  src="../../static/images/comment.png" class="comment-img"></image>
						<text>74123</text>
					</view>
				</view>
			</view> -->
		</view>
		<view v-else>
			<view class="details-null column-center">
				<image class="img-null" :src="`${imgPrefix}/images/xcxweb/goods_null.png`"></image>
				<view class="xs muted">该商品已下架或不存在，去逛逛别的吧~</view>
			</view>
			<!-- <recommend></recommend> -->
		</view>

		<!-- <share-popup v-model="showShareBtn" 
			:share-id="id" 
			pagePath="pages/goods_details/goods_details" 
			:type="1" 
			:config="{
				avatar: userInfo.avatar,
				nickname: userInfo.nickname,
				image: goodsDetail.poster || goodsDetail.image,
				price: goodsDetail.min_price,
				marketPrice: goodsDetail.market_price,
				name: goodsDetail.name
		}">
		</share-popup> -->
	

		<!-- <u-back-top :scroll-top="scrollTop" :top="1000" :customStyle="{ backgroundColor: '#FFF', color: '#000', boxShadow: '0px 3px 6px rgba(0, 0, 0, 0.1)'}"></u-back-top> -->
		
	</view>
</template>

<script>
	import {
		getGoodsDetail,
		addCart,
		getPoster,
		getCartNum,
		getArticleDetail
	} from '@/api/store';
	import {
		collectGoods
	} from '@/api/user';
	import {
		getGoodsCoupon,
		teamCheck
	} from '@/api/activity';
	import {
		mapActions,
		mapGetters
	} from 'vuex';
	import {
		arraySlice,
		trottle
	} from '@/utils/tools';
	import {
		toLogin
	} from '@/utils/login';
	import {
		getUser,
		inputInviteCode
	} from '@/api/user';
	import Cache from '@/utils/cache';
	import {
		strToParams
	} from '@/utils/tools'
	import { baseURL } from "@/config/app"
	
	export default {
		data() {
			return {
				scrollTop: 0,
				percent: 0,
				isFirstLoading: true,
				isNull: false,
				showSpec: false,
				showShareBtn: false,
				showCommission: true,
				popupType: '',
				swiperList: [],
				goodsDetail: {},
				goodsLike: [],
				goodsType: 0,
				checkedGoods: {},
				couponList: [],
				comment: {},
				countTime: 0,
				tagStyle: {
					img: 'width:100%;'
				},
				team: {},
				teamFound: [],
				isGroup: 0,
				id: '',
				showDownload: false,
				distribution: {},
				mealList: [
					{
						id: 1,
						name: '小龙象特惠双人餐 ¥49'
					},
					{
						id: 2,
						name: '18.8代20元金券 ¥49'
					}
				],
				activeMeal: 1,
				imgPrefix: baseURL
			};
		},
		onLoad(options) {
			this.onPageScroll = trottle(this.onPageScroll, 500, this)
			if (options && options.scene) {
				let scene = strToParams(decodeURIComponent(options.scene));
				console.log(scene, decodeURIComponent(options.scene))
				options.id = scene.id;
			}
			// #ifdef H5
			if (options && options.isapp == 1) {
				this.showDownload = true;
			}
			// #endif
			if (!options.id) {
				return this.$toast({
					title: '缺少参数，无法查看商品'
				}, {
					tab: 3
				});
			} else {
				this.id = options.id;
			}
			this.getGoodsCouponFun();
			this.getCartNum();
		},
		onShow() {
			this.getArticleDetailFun();
		},
		onPageScroll(e) {
			const top = uni.upx2px(100)
			const {
				scrollTop
			} = e
			this.percent = scrollTop / top > 1 ? 1 : scrollTop / top
			this.scrollTop = scrollTop
		},
		methods: {
			...mapActions(['getCartNum']),
			handleCall(mobile){
				uni.makePhoneCall({
					phoneNumber: mobile,
					success: () => {
					  console.log('电话呼叫成功');
					},
					fail: (err) => {
					  console.error('电话呼叫失败', err);
					}
				});
			},
			handleMeal(id){
				this.activeMeal = id;
				console.log('this.activeMeal', this.activeMeal)
			},
			// 导航
			handleNavigation(lat, lng){
				console.log('lat, lng', lat, lng)
				uni.openLocation({
					latitude: lat*1,
					longitude: lng*1,
					success: function () {
						console.log('success');
					}
				});
			},
			async getArticleDetailFun() {
				const {
					data,
					code
				} = await getArticleDetail({
					id: this.id
				});
				if (code == 1) {
					
					this.goodsDetail = data;
					console.log('this.goodsDetail', this.goodsDetail)
					let {
						image,
					} = data;
					this.swiperList = image.split(',').map((item) => {
						return {
							uri: item
						}
					});
					console.log('this.swiperList', this.swiperList)
					
					
					this.$nextTick(() => {
						this.isFirstLoading = false;
					});
					
					// #ifdef H5
					let options = {
						shareTitle: data.name,
						shareLink: location.href + '&invite_code=' + this.userInfo.distribution_code,
						shareImage: data.image,
						shareDesc: data.remark
					};
					this.wxShare(options);
					// #endif
				} else {
					this.isNull = true
					this.isFirstLoading = false;
				}
			},
			async getGoodsCouponFun() {
				const {
					data,
					code
				} = await getGoodsCoupon({
					id: this.id
				});
				if (code == 1) {
					this.couponList = data;
				}
			},
			onChangeGoods(e) {
				console.log(e);
				this.checkedGoods = e.detail;
			},
			showSpecFun(type, id) {
				if (!this.isLogin) return toLogin();
				if (this.goodsType == 2 && [2, 3].includes(type)) {
					this.isGroup = 1;
					this.foundId = id;
				} else {
					this.isGroup = 0;
					this.foundId = '';
				}
				this.popupType = type;
				this.showSpec = true;
			},
			onBuy(e) {
				let {
					id,
					goodsNum
				} = e.detail;
				const {
					goodsType,
					team
				} = this;
				let goods = [{
					item_id: id,
					num: goodsNum
				}];
				const params = {
					goods,
				};
				this.showSpec = false;
				goodsType == 2 ? (params.teamId = team.team_id) : '';
				this.foundId ? (params.foundId = this.foundId) : '';
				uni.navigateTo({
					url: '/pages/confirm_order/confirm_order?data=' + encodeURIComponent((JSON.stringify(params)))
				})
				console.log(1111)
			},
			onConfirm(e) {
				const {
					team: {
						team_id
					}
				} = this;
				teamCheck({
					team_id,
					found_id: this.foundId
				}).then(res => {
					if (res.code == 1) {
						this.onBuy(e);
					}
				});
			},
			async onAddCart(e) {
				let {
					id,
					goodsNum
				} = e.detail;

				if (this.goodsType == 2) {
					// 拼团单独购买
					let goods = [{
						item_id: id,
						num: goodsNum
					}];
					uni.navigateTo({
						url: '/pages/confirm_order/confirm_order?data=' + encodeURIComponent((JSON.stringify({
							goods
						})))
					})
					return
				}
				const {
					code,
					data,
					msg
				} = await addCart({
					item_id: id,
					goods_num: goodsNum
				});
				if (code == 1) {
					this.getCartNum();
					this.$toast({
						title: msg,
						icon: 'success'
					});
					this.showSpec = false;
				}
			}
		},
		async onShareAppMessage() {
			const {
				goodsDetail,
				team,
				userInfo
			} = this;
			return {
				title: team.share_title || goodsDetail.name,
				imageUrl: goodsDetail.image,
				path: '/pages/goods_details/goods_details?id=' + this.id + "&invite_code=" + userInfo.distribution_code
			};
		},
		computed: {
			...mapGetters(['cartNum', 'userInfo']),
		}
	};
</script>

<style lang="scss" scoped>
	.goods-details {
		padding-bottom: calc(120rpx + env(safe-area-inset-bottom));
		
		.product-box{
			background: #FFF;
			padding: 26rpx 20rpx;
			.title{
				font-weight: 700;
				font-size: 36rpx;
				color: #333333;
				line-height: 52rpx;
				letter-spacing: 1px;
				margin-bottom: 12rpx;
			}
			.label{
				padding: 6rpx 16rpx;
				background: rgba(255,197,82,0.3);
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				font-size: 24rpx;
				color: #CC760C;
				line-height: 34rpx;
			}
			.desc{
				font-size: 28rpx;
				color: #666666;
				line-height: 40rpx;
				margin-bottom: 10rpx;
				margin-top: 14px;
			}
			.num-box{
				font-size: 24rpx;
				color: #999999;
				line-height: 34rpx;
				text{
					margin-right: 30rpx;
				}
			}
			.price-box{
				display: flex;
				align-items: baseline;
				margin-top: 22rpx;
				.price{
					font-weight: 700;
					font-size: 64rpx;
					color: #FF7125;
					line-height: 74rpx;
					margin-right: 28rpx;
				}
				.mark-price{
					font-weight: 400;
					font-size: 24rpx;
					color: #999999;
					line-height: 34rpx;
					text-decoration: line-through;
				}
			}
		}

		.details-null {
			padding-top: 140rpx;
			margin-bottom: 100rpx;
		}

		.spec {
			padding: 24rpx;

			.text {
				width: 100rpx;
			}
		}


		.details {
			.title {
				line-height: 88rpx;
				text-align: center;
			}

			&>.content {
				padding: 0 20rpx 20rpx;
				overflow: hidden;

				::v-deep image {
					vertical-align: middle;
				}

				// #ifdef H5
				::v-deep img {
					vertical-align: middle;
				}

				// #endif
				// #ifdef MP-WEIXIN || APP-PLUS
				::v-deep ._img {
					display: block;
				}

				// #endif
			}
		}
		.store-box{
			margin-top: 40rpx;
			padding: 26rpx 20rpx;
			background: #F3F7F7;
			border-radius: 8rpx;
			.store-item{
				.left{
					image{
						width: 80rpx;
						height: 80rpx;
						border-radius: 12rpx;
						margin-right: 18rpx;
					}
					.info{
						.info-tit{
							font-weight: 500;
							font-size: 28rpx;
							color: #333333;
							line-height: 40rpx;
							margin-bottom: 6rpx;
						}
						.address{
							width: 370rpx;
							font-size: 24rpx;
							color: #666666;
							line-height: 34rpx;
							overflow: hidden;
							white-space: nowrap;
							text-overflow: ellipsis;
						}
					}
				}
				.right{
					.right-item{
						font-size: 20rpx;
						color: #333333;
						line-height: 28rpx;
						image{
							width: 48rpx;
							height: 48rpx;
							margin-bottom: 6rpx;
						}
						&:first-child{
							margin-right: 38rpx;
						}
					}
				}
			}
		}
		.footer {
			height: 100rpx;
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			box-sizing: content-box;
			padding-bottom: env(safe-area-inset-bottom);
			padding: 6rpx 32rpx;
			
			.input-box{
				background: #F5F5F5;
				border-radius: 228rpx 228rpx 228rpx 228rpx;
				position: relative;
				padding: 10rpx 20rpx;
				flex: 1;
				margin-right: 38rpx;
				.edit-img{
					width: 29rpx;
					height: 27rpx;
					margin-right: 22rpx;
				}
				input{
					flex: 1;
				}
			}
			
			.num-box{
				.collect-box{
					margin-right: 36rpx;
					.collect-img{
						width: 32rpx;
						height: 30rpx;
						margin-right: 10rpx;
					}
					text{
						font-size: 20rpx;
						color: #666666;
						line-height: 28rpx;
					}
				}
				.comment-box{
					.comment-img{
						width: 32rpx;
						height: 30rpx;
						margin-right: 10rpx;
					}
					text{
						font-size: 20rpx;
						color: #666666;
						line-height: 28rpx;
					}
				}
			}
		}
	}
</style>
